<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="assigneeName">
        <el-input
          v-model="queryParams.assigneeName"
          placeholder="请输入负责人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优先级" prop="priority">
        <el-select v-model="queryParams.priority" placeholder="请选择优先级" clearable>
          <el-option label="低" value="0"></el-option>
          <el-option label="中" value="1"></el-option>
          <el-option label="高" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['task:mainTask:add']"
        >新增任务</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 任务列表标签页 -->
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="待接收任务" name="pending">
        <task-table 
          :loading="loading" 
          :task-list="taskList" 
          :total="total"
          :query-params="queryParams"
          @selection-change="handleSelectionChange"
          @accept-task="handleAcceptTask"
          @view-detail="handleViewDetail"
          @pagination="getList"
          show-accept-button
        />
      </el-tab-pane>
      
      <el-tab-pane label="进行中任务" name="inProgress">
        <task-table 
          :loading="loading" 
          :task-list="taskList" 
          :total="total"
          :query-params="queryParams"
          @selection-change="handleSelectionChange"
          @update-status="handleUpdateStatus"
          @view-detail="handleViewDetail"
          @pagination="getList"
          show-status-button
        />
      </el-tab-pane>
      
      <el-tab-pane label="已完成任务" name="completed">
        <task-table 
          :loading="loading" 
          :task-list="taskList" 
          :total="total"
          :query-params="queryParams"
          @selection-change="handleSelectionChange"
          @view-detail="handleViewDetail"
          @pagination="getList"
        />
      </el-tab-pane>
    </el-tabs>

    <!-- 任务详情对话框 -->
    <el-dialog :title="detailTitle" :visible.sync="detailOpen" width="800px" append-to-body>
      <task-detail 
        v-if="detailOpen"
        :task-id="currentTaskId"
        @close="detailOpen = false"
      />
    </el-dialog>

    <!-- 添加任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <task-form 
        v-if="open"
        :form="form"
        @submit="submitForm"
        @cancel="cancel"
      />
    </el-dialog>
  </div>
</template>

<script>
import { 
  listPendingTasks, 
  listInProgressTasks, 
  listCompletedTasks,
  acceptTask,
  updateTaskStatus,
  addMaintask
} from "@/api/task/maintask"
import TaskTable from './components/TaskTable'
import TaskDetail from './components/TaskDetail'
import TaskForm from './components/TaskForm'

export default {
  name: "TaskManagement",
  components: {
    TaskTable,
    TaskDetail,
    TaskForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 详情弹出层
      detailOpen: false,
      detailTitle: "任务详情",
      currentTaskId: null,
      // 当前激活的标签页
      activeTab: "pending",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        assigneeName: null,
        priority: null
      },
      // 表单参数
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true
      let apiCall
      
      switch(this.activeTab) {
        case 'pending':
          apiCall = listPendingTasks
          break
        case 'inProgress':
          apiCall = listInProgressTasks
          break
        case 'completed':
          apiCall = listCompletedTasks
          break
        default:
          apiCall = listPendingTasks
      }
      
      apiCall(this.queryParams).then(response => {
        this.taskList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    
    /** 标签页切换 */
    handleTabClick(tab) {
      this.activeTab = tab.name
      this.resetQuery()
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.queryParams.pageNum = 1
      this.getList()
    },
    
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加任务"
    },
    
    /** 表单重置 */
    reset() {
      this.form = {
        taskId: null,
        taskName: null,
        description: null,
        priority: 1,
        dueDate: null,
        assigneeId: null,
        assigneeName: null,
        status: 0,
        remark: null,
        subTaskList: []
      }
      this.resetForm("form")
    },
    
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addMaintask(this.form).then(response => {
            this.$modal.msgSuccess("新增成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    
    /** 接收任务 */
    handleAcceptTask(taskId) {
      this.$modal.confirm('是否确认接收该任务？').then(function() {
        return acceptTask(taskId)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("接收成功")
      }).catch(() => {})
    },
    
    /** 更新任务状态 */
    handleUpdateStatus(taskId, status) {
      updateTaskStatus(taskId, status).then(response => {
        this.$modal.msgSuccess("状态更新成功")
        this.getList()
      })
    },
    
    /** 查看详情 */
    handleViewDetail(taskId) {
      this.currentTaskId = taskId
      this.detailOpen = true
    }
  }
}
</script>

<style scoped>
.mb8 {
  margin-bottom: 8px;
}
</style>